//校验产品分类必输
function checkProdCateRequired(){
	$("#complateProducts").find("[for='addProduct']").remove();
		
	var len = $("#complateProducts").find("tbody").find("tr").length;
	if(len == 0){
		var errMsg = getLanMsgs("0ec22180-100233");
		var errLabel = $("<img></img>").attr("for", "addProduct")
				.attr("src",
							"/ebvp/commonres/common/frame/src/img/validate_error.png")
				.addClass("error")
				.attr("title", errMsg).attr(
							"style",
							"position:absolute; left:92px; top:5px;");
		errLabel.insertAfter($("#addProduct"));
		return false;
	}else{
		return true;
	}
}


// 获得字符串的字节数
function getBytesCount(str){
	if(!str){
		return 0;
	}else{
		return (str.length + str.replace(/[\u0000-\u00ff]/g, "").length);
	}
}
	
function checkRegfound() {
		$("#complateBaseInfo").find("[for='regfound']").remove();
		var val = $("#regfound").val();
		
		if (val) {
			if (val.indexOf(".") == -1) { // 不包含小数点
				if (val.length > 11) {
					var errMsg = getLanMsgs("0ec22180-100231");
					var errLabel = $("<img></img>").attr("for", "regfound")
							.attr("src",
									"/ebvp/commonres/common/frame/src/img/validate_error.png")
							.addClass("error")
							.attr("title", errMsg).attr(
									"style",
									"position:absolute; left:178px");
					errLabel.insertAfter($("#regfound"));
					return false;
				}
			} else {
				var errMsg = getLanMsgs("0ec22180-100231");
				var valArray = val.split(".");
				var intPart = valArray[0];
				var dotPart = valArray[1];
				if (intPart.length > 11 || dotPart.length > 2) {
					var errLabel = $("<img></img>").attr("for", "regfound")
							.attr("src",
									"/ebvp/commonres/common/frame/src/img/validate_error.png")
							.addClass("error")
							.attr("title", errMsg).attr(
									"style",
									"position:absolute; left:178px");
					errLabel.insertAfter($("#regfound"));
					return false;
				}
			}
		}
		
		return true;
	}
	
	$(document).ready(function() {
		
		$("#taxpayerid").bind("blur", function(){
			checkTaxPayerIdUnique();
		})
		
		checkBaseInfo();
		checkContactInfo();
		checkBankAccInfo();	
		checkProdInfo();
		checkQaInfo();
	
		$("#potentialSupSubmit").bind('click', function(){
			
			dealWithGenderProperty();  //处理联系人的性别信息
	
		//checkRegfound();           //校验注册资本
		checkDuplicateQa();        //校验资质分类
		checkProdCateRequired();   //校验产品必输项
		
		// 校验联系人邮箱
		$("[name='email']").each(function(){
			$(this).next("[for='email']").remove();
			if(!checkEmail($(this).val())){
				var errorImg = $("<img></img>")
					.attr("for", "email")
					.attr("title", getLanMsgs("0ec22180-100171"))
					.attr("src", "/ebvp/commonres/common/frame/src/img/validate_error.png")
					.addClass("error")
					.attr("style", "position:absolute; left:180px; top:1px;");
				errorImg.insertAfter($(this));
			}
		});
		// 公司的电子邮箱
		$("body").find("[for='corpEmail']").remove();
		if(!checkEmail($("#corpEmail").val())){
			var errorImg = $("<img></img>")
				.attr("for", "corpEmail")
				.attr("title", getLanMsgs("0ec22180-100171"))
				.attr("src","/ebvp/commonres/common/frame/src/img/validate_error.png")
				.addClass("error")
				.attr("style", "position:absolute; left:180px; top:1px");
			errorImg.insertAfter($("#corpEmail"));
		}
		// 成立日期
		$("body").find("[for='builddate']").remove();
		if($("#builddate").val()==null || $("#builddate").val()=="" || $("#builddate").val()=="*"){
			var errorImg = $("<img></img>")
				.addClass("error")
				.attr("for", "builddate")
				.attr("title", getLanMsgs("0ec22180-100167"))
				.attr("src","/ebvp/commonres/common/frame/src/img/validate_error.png")
				.attr("style", "position:relative; top:1px; z-index:3; ");
			errorImg.insertAfter($("#builddate"));
		}
		// 校验国家地区
		$("body").find("[for='conutryName']").remove();
		if($("#conutryName").val()==null || $("#conutryName").val()=="" || $("#conutryName").val()=="*"){
			var errorImg = $("<img></img>")
				.addClass("error")
				.attr("for", "conutryName")
				.attr("title", getLanMsgs("0ec22180-100167"))
				.attr("src","/ebvp/commonres/common/frame/src/img/validate_error.png")
				.attr("style", "position:relative; top:1px; z-index:3; ");
			errorImg.insertAfter($("#conutryName"));
		}
		
		//校验必须的资料类型字段
		checkRequiredDoctypeEle();
		
		if(
				$("[for='regfound']").length !=0 || 
					$("[for='conaddress']").length !=0 || 
						$("[for='addProduct']").length !=0 ||
							$("[for='email']").length !=0 ||
								$("[for='corpEmail']").length !=0 ||
										$("[for='url']").length !=0 ||
										$("[for='builddate']").length !=0 ||
										$("[for='conutryName']").length !=0 || 
										$("[for='qaDuplicate']").length !=0 ||
										$("[for='req_doctype_attach']").length !=0
		){
			return ;
		}
			
			$("#potentialSupForm").attr("action",ebvp_context+"/register/nopassCommitSupInfo");
			$("#potentialSupForm").submit();
		});
		
		
		
		// 检查文档的各个区域是否填写完整，如果填写完整，左侧的导航栏相应的区域打勾
		function checkBaseInfo() {
							var supname = $("#supname").val();
							var supsimname = $("#supsimname").val();
							var legalbody = $("#legalbody").val();
							var regfound = $("#regfound").val();
							var taxpayerid = $("#taxpayerid").val();
							var salelicenceno = $("#salelicenceno").val();
							var builddate = $("#builddate").val();

							if (supname != null && mytrim(supname) != ""
									&& supsimname != null
									&& mytrim(supsimname) != ""
									&& legalbody != null && mytrim(legalbody) != ""
									&& regfound != null && mytrim(regfound) != ""
									&& taxpayerid != null
									&& mytrim(taxpayerid) != ""
									&& salelicenceno != null
									&& mytrim(salelicenceno) != ""
									&& builddate != null && mytrim(builddate) != "") {

								$("#baseInfo").attr("class", "step_finished");
							}
						}
		
		function checkContactInfo(){
			var isContactInfoPartOneFinished = false;
			// $("[identifier='contactList']").length == 1 表示没有联系人信息；1：是模板，不是数据;
			// 因为潜在供应商允许没有联系人，所以，当没有联系人，或者有联系人，信息都填写完整的情况下，才表示这部分完成
			if($("[identifier='contactList']").length == 1  ||(isArrFullFilled("linkman") && isArrFullFilled("mobile")&& isArrFullFilled("comPhoneReg")&& isArrFullFilled("comPhoneNum")&& isArrFullFilled("email"))){
				isContactInfoPartOneFinished = true;
			}else{
				$("#contactInfo").attr("class", "");
			}

			var isContactInfoPartTwoFinished = false;
			var pk_country = $("#pk_country").val();
			var conaddress= $("#conaddress").val();
			var zipcode= $("#zipcode").val();
			var corpPhoneReg= $("#corpTelReg").val();
			var corpPhoneNum= $("#corpTelNum").val();
			var corpEmail= $("#corpEmail").val();
			if(
				pk_country!= null && mytrim(pk_country)!="" &&
				conaddress!= null && mytrim(conaddress)!="" &&
				zipcode!= null && mytrim(zipcode)!="" &&
				corpPhoneReg!= null && mytrim(corpPhoneReg)!="" &&
				corpPhoneNum!= null && mytrim(corpPhoneNum)!="" &&
				corpEmail!= null && mytrim(corpEmail)!="" 		
			){
				isContactInfoPartTwoFinished = true;
			}
			
			if(isContactInfoPartOneFinished && isContactInfoPartTwoFinished){
				$("#contactInfo").attr("class", "step_finished");
			}
		}
		
		function checkBankAccInfo(){
			if($("#complateBankAccount").find("tbody").find("tr").length>0){
				if(isArrFullFilled("supaccount") && isArrFullFilled("accountname")
						&& isArrFullFilled("bankTypeName")&& isArrFullFilled("bankDocName")&& isArrFullFilled("currencyName")){
					$("#bankAccountInfo").attr("class", "step_finished");
				}
			}
		}
		
		function checkProdInfo(){
			if($("#productTable").find("tr").length != 1){
				$("#prodDescriptionInfo").attr("class", "step_finished");
			}
		}
		
		function checkQaInfo(){
			if($("#complateQualification").find("[name='pk_supcheckqa']").length > 0){
				$("#qaInfo").attr("class", "step_finished");
			}
		}
		
		function isArrFullFilled(eleName){
			var isFullFilled = true;
			$("[name='"+eleName+"']").each(
					function(){
						if( !$(this).hasClass("tpl") && ($(this).val()==null ||mytrim($(this).val())=="")){
							isFullFilled = false;
						} 
					}
			);
			return isFullFilled;
		}
		
		// trim
		function mytrim(strToTrim) {
			var result = strToTrim;
			for (var _i = 0; _i < strToTrim.length; _i++) {
				if (strToTrim.charAt(_i) == " "){
					result = strToTrim.substr(_i + 1);
				}else{
					_i = strToTrim.length;
				}
			}
			strToTrim = result;
			for (var _i = strToTrim.length - 1; _i >= 0; _i--) {
				if (strToTrim.charAt(_i) == " "){
					result = strToTrim.substr(0, _i);
				}else{
					_i = 0;
				}
			}
			return result;
		}
		// 校验资质分类是否重复
		function checkDuplicateQa() {
			$("body").find("[for='qaDuplicate']").remove();
			var Options = $("#complateQualification").find("option:selected");
			for ( var loop = 0; loop < Options.length; loop++) {
				var qaVal = $(Options[loop]).val();
				for ( var loopY = loop + 1; loopY < Options.length; loopY++) {
					var tempQa = $(Options[loopY]).val();
					if (tempQa == qaVal) {
						var errorImg = $("<img></img>")
							.addClass("error")
							.attr("for", "qaDuplicate")
							.attr("title", getLanMsgs("0ec22180-200189"))
							.attr("src",
										"/ebvp/commonres/common/frame/src/img/validate_error.png")
							.attr("style",
											"position:relative; z-index:3; ");
						errorImg.insertAfter($("#addQaType"));
						return false; //校验没通过，有重复的资质分类
					}
				}
			}
			return true; //校验通过，没有重复的资质分类
		}
		
		//经营管理
		$( "#managementAdd" ).click(function() {
		    $( "#managementInfo:visible" ).slideUp();
		    $( "#managementInfo:hidden" ).slideDown(function() { $( this ).removeClass("hidden"); });
		});
		
		//销售情况
		$( "#saleInfoAdd" ).click(function() {
		    $( "#saleInfo:visible" ).slideUp();
		    $( "#saleInfo:hidden" ).slideDown(function() { $( this ).removeClass("hidden"); });
		});
		
		//公司基本情况
		$( "#companyBaseInfoAdd" ).click(function() {
		    $( "#companyBaseInfo:visible" ).slideUp();
		    $( "#companyBaseInfo:hidden" ).slideDown(function() { $( this ).removeClass("hidden"); });
		});
		
		//分销/售后服务中心
		$( "#afterServiceCenterAdd" ).click(function() {
		    $( "#afterServiceCenter:visible" ).slideUp();
		    $( "#afterServiceCenter:hidden" ).slideDown(function() { $( this ).removeClass("hidden"); });
		});
		
		//订购
		$( "#orderAdd" ).click(function() {
		    $( "#order:visible" ).slideUp();
		    $( "#order:hidden" ).slideDown(function() { $( this ).removeClass("hidden"); });
		});
		
		//是否有最小订购量的值改变时，调整最小订购量的必输性
		$("[name='fisminorder']").change(function() {
			if($("[name='fisminorder']").val()=="1") {
				$("[name='nminordernum']").parent('label').find('.revert').remove(); //去掉先前加的必输星
				$("[name='nminordernum']").UITipInside( {   //添加必输星
					'required' : true
				});
			} else {
				$("[name='nminordernum']").UITipInside();   //去掉必输星  
			}
		
		});
});